package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.core.common.util.PinYinUtils;
import com.joysuch.wwyt.core.entity.BaseTypeGroup;
import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import com.joysuch.wwyt.core.repository.BaseTypeGroupDao;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import com.joysuch.wwyt.core.support.Context;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author zero
 * @since 2021/11/18 14:20
 */
@Component
public class ImportRiskDistrictTypeConvertor implements FieldValueConvert<String, Long> {

    @Autowired
    private BaseTypeGroupDao baseTypeGroupDao;
    @Autowired
    private BaseTypeGroupItemDao baseTypeGroupItemDao;

    @Override
    public Long convert(Object bean, String value, DataDumpField dataField) {
        //value为null直接返回不校验
        if(StringUtils.isBlank(value)){
            return null;
        }
        //先找出导入的类型是否存在
        BaseTypeGroup riskDistrict = baseTypeGroupDao.findByName("风险分析对象类型");
        if (riskDistrict == null){
            throw new IllegalArgumentException("请配置字典风险区域类型");
        }
        BaseTypeGroupItem baseTypeGroupItem = baseTypeGroupItemDao.findFirstByTypeGroupIdAndName(riskDistrict.getId(), value);
        if (baseTypeGroupItem != null){
            return baseTypeGroupItem.getId();
        }
        BaseTypeGroupItem groupItem = new BaseTypeGroupItem();
        groupItem.setName(value);
        groupItem.setTenentId(Context.getCurrentTenantId());
        groupItem.setOrgCode(Context.getCurrentOrgCode());
        groupItem.setTypeGroupId(riskDistrict.getId());
        groupItem.setShowOrder(1);
        groupItem.setState(1);
        String abbrev = riskDistrict.getCode();
        String itemCode = PinYinUtils.getHanziInitials(groupItem.getName());
        //如果字典名称无法解析成汉语拼音则后缀用字段名称
        if (StringUtils.isNotBlank(itemCode)) {
            abbrev = abbrev + "_" + PinYinUtils.getHanziInitials(groupItem.getName());
        } else {
            abbrev = abbrev + "_" + groupItem.getName();
        }
        //字典名称如果已存在则在后面添加数字
        List<BaseTypeGroupItem> giList = baseTypeGroupItemDao.getListByCodeLike(abbrev);
        if (!CollectionUtils.isEmpty(giList)) {
            abbrev = abbrev + (giList.size() + 1);
        }
        groupItem.setCode(abbrev);
        groupItem.setState(DataStateTypes.EFFECTIVE.getState());
        List<BaseTypeGroupItem> iList = baseTypeGroupItemDao.findByNameAndTypeGroupId(groupItem.getName(), groupItem.getTypeGroupId());
        if (iList.size() > 1) {

            return null;
        }
        BaseTypeGroupItem save = baseTypeGroupItemDao.save(groupItem);
        return save.getId();
    }
}
